package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.admin.entity.AdSensitive;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.PageResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.model.admin.dtos.SensitiveDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2022-09-13
 */
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Autowired
    AdSensitiveMapper adSensitiveMapper;
    /**
     * 分页查询敏感词
     * @param dto
     * @return
     */
    @Override
    public PageResult<AdSensitive> findByPage(SensitiveDto dto) {
        //分页设置
        Page<AdSensitive> ipage = new Page<>(dto.getPage(),dto.getSize());
        //设置查询
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(dto.getSensitives()),AdSensitive::getSensitives,dto.getSensitives());

        //根据时间倒序查询
        queryWrapper.orderByAsc(AdSensitive::getCreatedTime);
        IPage<AdSensitive> adSensitaivePage =page(ipage,queryWrapper);

        //判断信息是否为空
        if (CollectionUtils.isEmpty(adSensitaivePage.getRecords())){
            throw new LeadException(AppHttpCodeEnum.SENSITIVE_NOT_FOUND);
        }
        return new PageResult<>(dto.getPage(),dto.getSize(),adSensitaivePage.getTotal(),adSensitaivePage.getRecords());
    }
}
